//
//  HZWallData.m
//  Model
//
//  Created by zeb-apple on 2017/5/24.
//  Copyright © 2017年 YDJH. All rights reserved.
//

#import "HZWallData.h"
#import "RoomData.h"
#import "FurnitureData.h"
#import "BuildingConstructionLogicSystem.h"
#import "HZFurniture_Header.h"


@implementation HZWallData


//室外墙生成
+ (HZOutsideWall *)goBackOutsideWallWithRoom:(HZRoom *)room indexPath:(NSIndexPath *)indexPath maxsection:(NSInteger)maxsection maxrow:(NSInteger)maxrow picture:(NSString *)picture bottomRoom:(HZRoom *)bottomRoom {
    
    HZOutsideWall *outsideWall = [[HZOutsideWall alloc] init];
    outsideWall.wallPicture = picture; //背景图
    
    RoomData *roomData = room.roomData;
    NSArray *outsideRoomFurniture = roomData.outsideRoomFurniture;
    outsideWall.furnitureArr = (NSMutableArray *)[FurnitureData goBackArrayWithOutsideFutiniture:outsideRoomFurniture withFurData:[[BuildingConstructionLogicSystem standardBuildingConstructionLogicSystem] furnitureDataArr] indexPath:indexPath];
    
    outsideWall.position = indexPath;
    outsideWall.canLeft = YES;
    outsideWall.canRight = YES;
    
    HZFurniture *lastLabber = nil;
    if (bottomRoom) {
        lastLabber = [self goBackOutsideUpladderFromRoom:bottomRoom];
        lastLabber.otherposition = roomData.outsideLadderPosition;
        if ([lastLabber.furnitureState isEqualToString:destory_Type]) {
            outsideWall.canDown = NO;
        }else {
            outsideWall.canDown = YES;
        }
    }
    if ([room.type isEqualToString:Stair_Room]) { //楼梯间
        
        if (indexPath.row == 0) { //第一列
            outsideWall.canLeft = NO;
            if (indexPath.section == 0) { //第一层 //没有下楼梯
                for (HZFurniture * _Nonnull obj in [outsideWall.furnitureArr reverseObjectEnumerator]) {
                    if ([obj.furnitureType isEqualToString:OutsideUpStairs]) {//判断上梯是否损坏
                        if ([obj.furnitureState isEqualToString:destory_Type]) {
                            outsideWall.canUp = NO;
                        }else {
                            outsideWall.canUp = YES;
                        }
                    }else if ([obj.furnitureName rangeOfString:Window_Type].location != NSNotFound) { //删除窗户
                        [outsideWall.furnitureArr removeObject:obj];
                    }
                }
            }else if (indexPath.section == maxsection-1) { //最上层
                for (HZFurniture * _Nonnull obj in [outsideWall.furnitureArr reverseObjectEnumerator]) {
                    if ([obj.furnitureType isEqualToString:OutsideUpStairs]) { //删除上梯
                        [outsideWall.furnitureArr removeObject:obj];
                    }else if ([obj.furnitureName rangeOfString:Door_Type].location != NSNotFound) { //删除门🚪
                        [outsideWall.furnitureArr removeObject:obj];
                    }
                }
                if (lastLabber) {
                  [outsideWall.furnitureArr addObject:lastLabber];
                }
            }else {
                for (HZFurniture * _Nonnull obj in [outsideWall.furnitureArr reverseObjectEnumerator]) {
                    if ([obj.furnitureType isEqualToString:OutsideUpStairs]) {
                        if ([obj.furnitureState isEqualToString:destory_Type]) {
                            outsideWall.canUp = NO;
                        }else {
                            outsideWall.canUp = YES;
                        }
                    }else if ([obj.furnitureName rangeOfString:Door_Type].location != NSNotFound) { //删除门🚪
                        [outsideWall.furnitureArr removeObject:obj];
                    }
                if (lastLabber) {
                    [outsideWall.furnitureArr addObject:lastLabber];
                }
            }
            }
        }else { //最后一列 不会有外梯
            
            outsideWall.canRight = NO;
            for (HZFurniture *furniture in [outsideWall.furnitureArr reverseObjectEnumerator]) {
                if ([furniture.furnitureType isEqualToString:OutsideUpStairs]) { //删除外梯
                    [outsideWall.furnitureArr removeObject:furniture];
                }else if ([furniture.furnitureName rangeOfString:Door_Type].location != NSNotFound) { //删除后门
                    [outsideWall.furnitureArr removeObject:furniture];
                }
                outsideWall.canUp = NO;
                outsideWall.canDown = NO;
            }
            
        }
    }else {
        outsideWall.canDown = NO;
        outsideWall.canUp = NO;
    }
    
    
    return outsideWall;
    
}
//大厅室外墙生成
+ (HZOutsideWall *)goBackOutsideWallWithLobbyRoom:(HZRoom *)room indexPath:(NSIndexPath *)indexPath maxsection:(NSInteger)maxsection maxrow:(NSInteger)maxrow picture:(NSString *)picture bottomRoom:(HZRoom *)bottomRoom {
    
    HZOutsideWall *outsideWall = [[HZOutsideWall alloc] init];
    outsideWall.wallPicture = picture; //背景图
    
    RoomData *roomData = room.roomData;
    NSArray *outsideRoomFurniture = roomData.outsideRoomFurniture;
    outsideWall.furnitureArr = (NSMutableArray *)[FurnitureData goBackArrayWithOutsideFutiniture:outsideRoomFurniture withFurData:[[BuildingConstructionLogicSystem standardBuildingConstructionLogicSystem] furnitureDataArr] indexPath:indexPath];
    
    outsideWall.position = indexPath;
    outsideWall.canLeft = NO;
    outsideWall.canRight = NO;
    
    HZFurniture *lastLabber = nil;
    if (bottomRoom) {
        lastLabber = [self goBackOutsideUpladderFromRoom:bottomRoom];
        lastLabber.otherposition = roomData.outsideLadderPosition;
        if ([lastLabber.furnitureState isEqualToString:destory_Type]) {
            outsideWall.canDown = NO;
        }else {
            outsideWall.canDown = YES;
        }
    }
    if (room.isFirstSurfaceRoom) { //第一层
        for (HZFurniture * _Nonnull obj in [outsideWall.furnitureArr reverseObjectEnumerator]) {
            if ([obj.furnitureType isEqualToString:OutsideUpStairs]) {
                if ([obj.furnitureState isEqualToString:destory_Type]) {
                    outsideWall.canUp = NO;
                }else {
                    outsideWall.canUp = YES;
                }
            }else if ([obj.furnitureName rangeOfString:Window_Type].location != NSNotFound) { //删除窗户
                [outsideWall.furnitureArr removeObject:obj];
            }
            outsideWall.canDown = NO;
        }
    }else if (indexPath.section == maxsection-1) { //最上层
        for (HZFurniture * _Nonnull obj in [outsideWall.furnitureArr reverseObjectEnumerator]) {
            if ([obj.furnitureType isEqualToString:OutsideUpStairs]) { //删除上外梯
              [outsideWall.furnitureArr removeObject:obj];
            }else if ([obj.furnitureName rangeOfString:Door_Type].location != NSNotFound) { //删除门🚪
                [outsideWall.furnitureArr removeObject:obj];
            }
            
        }
        if (lastLabber) { //添加下外梯
            [outsideWall.furnitureArr addObject:lastLabber];
        }
    }else { //中间层
        for (HZFurniture * _Nonnull obj in [outsideWall.furnitureArr reverseObjectEnumerator]) {
            if ([obj.furnitureType isEqualToString:OutsideUpStairs]) {
                if ([obj.furnitureState isEqualToString:destory_Type]) {
                    outsideWall.canUp = NO;
                }else {
                    outsideWall.canUp = YES;
                }
            }else if ([obj.furnitureName rangeOfString:Door_Type].location != NSNotFound) { //删除门🚪
                [outsideWall.furnitureArr removeObject:obj];
            }
        }
        if (lastLabber) {
            [outsideWall.furnitureArr addObject:lastLabber];
        }
    }
    
    return outsideWall;
    
}

//室内墙生成  主要门的生成
+ (HZInsideWall *)goBackInsideWallWithRoom:(HZRoom *)room lastRoom:(HZRoom *)lastRoom outsideWall:(HZOutsideWall *)outsideWall indexPath:(NSIndexPath *)indexPath maxsection:(NSInteger)maxsection maxrow:(NSInteger)maxrow bottomRoom:(HZRoom *)bottomRoom {
    
    HZInsideWall *insideWall = [[HZInsideWall alloc] init];
    
    RoomData *roomData = room.roomData;
    //上个房间的墙
    HZInsideWall *lastInsideWall = lastRoom.insidewall1;
    
    //墙的背景图
    insideWall.wallPicture = roomData.insidePicture;
    
    //返回外墙上的窗户
    HZFurniture *window = [self goBackInsidekWindowFromOutsideWall:outsideWall];
    //返回外墙上的门
    HZFurniture *door = [self goBackInsidekDoorFromOutsideWall:outsideWall];
    
    //拿到当前房间的数组
    NSMutableArray *insideRoomFurniture = roomData.insideRoomFurniture;
    
    //拿到上个房间的墙的家具对象数组
    NSMutableArray *lastFurniture = lastInsideWall.furnitureArr;
    
    //返回当前房间家具对象 HZFurniture 全部给该房间的墙
    NSMutableArray *furnitureArr = (NSMutableArray *)[FurnitureData goBackArrayWithinsideFutiniture:insideRoomFurniture withFurData:[[BuildingConstructionLogicSystem standardBuildingConstructionLogicSystem] furnitureDataArr] indexPath:indexPath];
    
    
    if (indexPath.section == maxsection-1) { //最上层没有上楼梯
        for (HZFurniture *furniture in furnitureArr) {
            if ([furniture.furnitureType isEqualToString:UpStaircase]) {
                [furnitureArr removeObject:furniture];
            }
        }
    }
    
    if (indexPath.row == 0) { //每层第一间房 楼梯间
        if (bottomRoom) {
            if ([bottomRoom.roomCategory isEqualToString:RoomCategory_InsideLobby]) { //下层房间是大厅
                [bottomRoom.insidewall1.furnitureArr enumerateObjectsUsingBlock:^(HZFurniture * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                    if ([obj.furnitureType isEqualToString:UpStaircase]) {
                        
                        NSArray *arr = [obj.position componentsSeparatedByString:@","];
                        NSInteger x = [[arr firstObject] integerValue];
                        if (x < SCREEN_WIDTH/2) { //左边
                            *stop = YES;
                            obj.otherposition = roomData.insideLadderPosition1;
                            obj.position_type = Staircase_left;
                            [furnitureArr addObject:obj];
                        }
                    }
                }];
            }else { //普通楼梯间
                [bottomRoom.insidewall1.furnitureArr enumerateObjectsUsingBlock:^(HZFurniture * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                    if ([obj.furnitureType isEqualToString:UpStaircase]) {
                        *stop = YES;
                        obj.otherposition = roomData.insideLadderPosition1;
                        [furnitureArr addObject:obj];
                    }
                }];
            }
        }
    }else if (indexPath.row == maxrow - 1) { //每层最后一间房
        
        if (indexPath.row >= 3) {//最后一间也是楼梯间
            
            if (bottomRoom) {
                if ([bottomRoom.roomCategory isEqualToString:RoomCategory_InsideLobby]) { //下层房间是大厅
                    [bottomRoom.insidewall1.furnitureArr enumerateObjectsUsingBlock:^(HZFurniture * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                        if ([obj.furnitureType isEqualToString:UpStaircase]) {
                            
                            NSArray *arr = [obj.position componentsSeparatedByString:@","];
                            NSInteger x = [[arr firstObject] integerValue];
                            if (x > SCREEN_WIDTH/2) { //右边
                                *stop = YES;
                                obj.otherposition = roomData.insideLadderPosition1;
                                obj.position_type = Staircase_right;
                                [furnitureArr addObject:obj];
                            }
                        }
                    }];
                }else { //普通楼梯间
                    [bottomRoom.insidewall1.furnitureArr enumerateObjectsUsingBlock:^(HZFurniture * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                        if ([obj.furnitureType isEqualToString:UpStaircase]) {
                            *stop = YES;
                            obj.otherposition = roomData.insideLadderPosition1;
                            [furnitureArr addObject:obj];
                        }
                    }];
                }
            }
            //拿到上个房间的门
            HZFurniture *door = [[self goBackDoorFromInsideRoomFurniture:lastFurniture] copy];
            door.indexPath = indexPath;
            door.otherposition = roomData.insideRightDoorPosition;
            //添加到上个房间
            [lastFurniture addObject:door];
            //添加到当前房间
            [furnitureArr addObject:door];
        }else {
            //返回当前房间的门
            HZFurniture *door = [self goBackDoorFromInsideRoomFurniture:furnitureArr];
            door.otherposition = roomData.insideRightDoorPosition;
            [lastFurniture addObject:door];
        }
        
    }else { //除第一间房 和最后一间房的其它房间
        
        //返回当前房间的门
        HZFurniture *door = [self goBackDoorFromInsideRoomFurniture:furnitureArr];
        door.otherposition = roomData.insideRightDoorPosition;
        [lastFurniture addObject:door];
    }
    
    
    //如果有窗户添加房间内
    if (window) {
        window.otherposition = roomData.insideRightWindowPosition;
        [furnitureArr addObject:window];
    }
    if (door) {
        door.otherposition = roomData.insideBackDoorPosition;
        [furnitureArr addObject:door];
    }
    insideWall.furnitureArr = furnitureArr;
    
    return insideWall;
    
}
//大厅室内墙生成
+ (HZInsideWall *)goBackInsideWallWithLobbyRoom:(HZRoom *)room outsideWall:(HZOutsideWall *)outsideWall indexPath:(NSIndexPath *)indexPath maxsection:(NSInteger)maxsection bottomRoom:(HZRoom *)bottomRoom {
    
    HZInsideWall *insideWall = [[HZInsideWall alloc] init];
    
    RoomData *roomData = room.roomData;
    
    //返回外墙上的窗户
    HZFurniture *window = [self goBackInsidekWindowFromOutsideWall:outsideWall];
    //返回外墙上的门
    HZFurniture *door = [self goBackInsidekDoorFromOutsideWall:outsideWall];

    //墙的背景图
    insideWall.wallPicture = roomData.insidePicture;
    
    NSMutableArray *furnitureArr = (NSMutableArray *)[FurnitureData goBackArrayWithinsideFutiniture:roomData.insideRoomFurniture withFurData:[[BuildingConstructionLogicSystem standardBuildingConstructionLogicSystem] furnitureDataArr] indexPath:indexPath];
    
    if (indexPath.section == maxsection-1) { //最上层没有上楼梯
        for (HZFurniture *furniture in furnitureArr) {
            if ([furniture.furnitureType isEqualToString:UpStaircase]) {
                [furnitureArr removeObject:furniture];
            }
        }
    }
    
    if (bottomRoom) {
        for (HZFurniture *furniture in bottomRoom.insidewall1.furnitureArr) {
            if ([furniture.furnitureType isEqualToString:UpStaircase]) {
                NSArray *arr = [furniture.position componentsSeparatedByString:@","];
                NSInteger x = [[arr firstObject] integerValue];
                if (x < SCREEN_WIDTH/2) { //左边
                    furniture.otherposition = roomData.insideLadderPosition1;
                    furniture.position_type = Staircase_left;
                    [furnitureArr addObject:furniture];
                }else { //右边
                    furniture.otherposition = roomData.insideLadderPosition2;
                    furniture.position_type = Staircase_right;
                    [furnitureArr addObject:furniture];
                }
                
            }
        }
        
    }
    
    //有窗户添加到内窗
    if (window) {
        window.otherposition = roomData.insideRightWindowPosition;
        [furnitureArr addObject:window];
    }
    if (door) {
        door.otherposition = roomData.insideBackDoorPosition;
        [furnitureArr addObject:door];
    }
    insideWall.furnitureArr = furnitureArr;
    return insideWall;
    
}
//室外墙返回单色外墙
+ (HZOutsideWall *)goBackOutsideWallWithMonochromaticWallPicture:(NSString *)picture {
    HZOutsideWall *outsideWall = [[HZOutsideWall alloc] init];
    outsideWall.wallPicture = picture;
    outsideWall.canUp = NO;
    outsideWall.canDown = NO;
    outsideWall.canLeft = NO;
    outsideWall.canRight = NO;
    return outsideWall;
}
//室外墙返回主墙
+ (HZOutsideWall *)goBackOutsideWallWithMainEntranceWallPicture:(NSString *)picture furnitureArr:(NSMutableArray <HZFurniture *>*)furnitureArr indexPath:(NSIndexPath *)indexPath {
    HZOutsideWall *outsideWall = [[HZOutsideWall alloc] init];
    outsideWall.wallPicture = picture;
    outsideWall.furnitureArr = (NSMutableArray *)[FurnitureData goBackArrayWithOutsideFutiniture:furnitureArr withFurData:[[BuildingConstructionLogicSystem standardBuildingConstructionLogicSystem] furnitureDataArr] indexPath:indexPath];
    outsideWall.canUp = NO;
    outsideWall.canDown = NO;
    outsideWall.canLeft = NO;
    outsideWall.canRight = NO;
    return outsideWall;
}
//室内房间家具返回该房间的门
+ (HZFurniture *)goBackDoorFromInsideRoomFurniture:(NSMutableArray *)insideRoomFurniture {
    
    __block HZFurniture *door = nil;
    [insideRoomFurniture enumerateObjectsUsingBlock:^(HZFurniture * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        if ([obj.furnitureName rangeOfString:Door_Type].location != NSNotFound) {
            *stop = YES;
            door = obj;
        }
    }];
    return door;
}
//室外墙上的窗户返回到室内窗户
+ (HZFurniture *)goBackInsidekWindowFromOutsideWall:(HZOutsideWall *)outsideWall {
    
    __block HZFurniture *window = nil;
    [outsideWall.furnitureArr enumerateObjectsUsingBlock:^(HZFurniture * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        if ([obj.furnitureName rangeOfString:Window_Type].location != NSNotFound) {
            *stop = YES;
            window = obj;
        }
    }];
    return window;
}
//室外墙上的门返回到室内门
+ (HZFurniture *)goBackInsidekDoorFromOutsideWall:(HZOutsideWall *)outsideWall {
    
    __block HZFurniture *window = nil;
    [outsideWall.furnitureArr enumerateObjectsUsingBlock:^(HZFurniture * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        if ([obj.furnitureName rangeOfString:Door_Type].location != NSNotFound) {
            *stop = YES;
            window = obj;
        }
    }];
    return window;
}
//在下层房间中找到上外梯
+ (HZFurniture *)goBackOutsideUpladderFromRoom:(HZRoom *)room {
    
    HZOutsideWall *outsideWall = room.outsideWall1;
    __block HZFurniture *ladder = nil;
    [outsideWall.furnitureArr enumerateObjectsUsingBlock:^(HZFurniture * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        if ([obj.furnitureType isEqualToString:OutsideUpStairs]) { //上外梯
            *stop = YES;
            ladder = obj;
        }
    }];
    return ladder;
}
@end
